package danling

import (
	"encoding/base64"
	"encoding/json"
	"gitee.com/thingple/detonator/rest"
	"github.com/lishimeng/go-log"
)

type LshUidProjectInfo struct {
	LowVoltageId string `json:"lowVoltageId,omitempty"` // 流水号 50Byte
	Uid          string `json:"uid,omitempty"`          // UID 30Byte
}

func (c *client) RegisterUid(projects ...LshUidProjectInfo) (code int, err error) {

	req := Req{}
	req.Xpch = c.xpch

	for _, project := range projects {
		req.Projects = append(req.Projects, project)
	}

	var resp Resp

	bs, err := json.Marshal(req)
	if err != nil {
		return
	}
	//tmp := strings.ReplaceAll(string(bs), " ", "")
	log.Info("req:%s", bs)
	reqBs, err := encode(bs, c.desKey)
	if err != nil {
		return
	}
	reqContent := base64.StdEncoding.EncodeToString(reqBs)
	log.Info("req:%s", reqContent)
	params := make(map[string]string)
	params["param"] = reqContent
	code, err = rest.NewRest(c.host).Path("lsh_uid_Servlet", "addLsh_Uid").ResponseJson(&resp).FormUrl(params)
	if err != nil {
		return
	}
	return
}
